package net.xiangcaowuyu.springcloud.alibaba.seata.service.impl;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import net.xiangcaowuyu.springcloud.alibaba.seata.dao.OrderDao;
import net.xiangcaowuyu.springcloud.alibaba.seata.domain.Order;
import net.xiangcaowuyu.springcloud.alibaba.seata.service.IAccountService;
import net.xiangcaowuyu.springcloud.alibaba.seata.service.IOrderService;
import net.xiangcaowuyu.springcloud.alibaba.seata.service.IStorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 博客：https://www.xiangcaowuyu.net
 * Description:
 *
 * @Author: 香草物语
 * DateTime: 2021-07-25 20:27
 */
@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private IStorageService storageService;

    @Resource
    private IAccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp-create-order")
//    @Transactional
    public int create(Order order) {
        log.info("--------->开始新建订单");
        orderDao.create(order);
        log.info("--------->订单微服务开始调用库存，做扣减Count");
        storageService.storageDecrease(order.getProductId(), order.getCount());
        log.info("--------->订单微服务开始调用库存，做扣减End");
        log.info("--------->订单微服务开始调用账户，做扣减Money");
        accountService.accountDecrease(order.getUserId(), order.getMoney());
        log.info("--------->订单微服务开始调用账户，做扣减End");
        log.info("--------->修改订单状态开始");
        orderDao.update(order.getUserId(), 0);
        log.info("--------->修改订单状态End");
        return 1;
    }
}
